package week_06;

import java.util.Arrays;

public class MaxProfit_Frozen_309 {
    public int maxProfit(int[] prices) {
        int n = prices.length;

        int[] pricesInput = new int[n + 1];
        pricesInput[0] = 0;
        for (int i = 0; i < n; i++) {
            pricesInput[i + 1] = prices[i];
        }


        int[][][] dp = new int[n + 1][2][2];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i][0], -1000000000);
            Arrays.fill(dp[i][1], -1000000000);
        }

        dp[0][0][0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                for (int d = 0; d < 2; d++) {
                    // 三种状态转移:
                    // case1: 不卖不买 rest 休息
                    // case3: 如果空仓可以买入
                    System.out.println(dp[i][j][d]);
                    System.out.println(pricesInput[i + 1]);
                    if (j == 0 && d == 0) dp[i + 1][1][0] = Math.max(dp[i + 1][1][0], dp[i][j][d] - pricesInput[i + 1]);
                    // case2: 如果持仓且冷冻期为0 则可以卖出，冷冻期加一
                    if (j == 1 && d == 0) dp[i + 1][0][1] = Math.max(dp[i + 1][0][1], dp[i][j][d] + pricesInput[i + 1]);

                    dp[i + 1][j][0] = Math.max(dp[i + 1][j][0], dp[i][j][d]);
                    System.out.println("i:" + i + ":j:" + j + ":d:" + d + ":" + dp[i][j][d]);
                }
            }
        }

        int ans = 0;
        for (int d = 0; d <= 1; d++) {
            System.out.println(dp[n][0][d]);
            ans = Math.max(dp[n][0][d], ans);
        }
        return ans;
    }


    public int maxProfit_1(int[] prices) {
        int n = prices.length;
        int[][][] dp = new int[n + 1][2][2];
        int[] pricesInput = new int[n + 1];
        pricesInput[0] = 0;
        for (int i = 0; i < n; i++) {
            pricesInput[i + 1] = prices[i];
        }
        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i][0], -1000000000);
            Arrays.fill(dp[i][1], -1000000000);
        }
        dp[0][0][0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < 2; j++) {
                for (int d = 0; d < 2; d++) {
                    // 三种状态转移:
                    // case1: 不卖不买 rest休息-> 前一天不管啥状态都更新一遍可能的最大值
                    dp[i][j][0] = Math.max(dp[i][j][0], dp[i - 1][j][d]);
                    // case2: i-1天如果持仓且冷冻期为0 则可以卖出，i天则，持仓0 冷冻期 1
                    if (j == 0 && d == 1) dp[i][0][1] = Math.max(dp[i][0][1], dp[i - 1][1][0] + pricesInput[i]);
                    // case3: i-1天如果空仓可以买入，买完 i天则持仓1,冷冻期0
                    if (j == 1 && d == 0) dp[i][1][0] = Math.max(dp[i][1][0], dp[i - 1][0][0] - pricesInput[i]);
                }
            }
        }
        int ans = Integer.MIN_VALUE;
        for (int d = 0; d <= 1; d++) {
            ans = Math.max(dp[n][0][d], ans);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 0, 2};
        MaxProfit_Frozen_309 test = new MaxProfit_Frozen_309();
        test.maxProfit(arr);
    }
}
